package com.yuchen.advsdkdemo;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import com.yuchen.advsdk.manager.LayoutPollManager;
//import com.yuchen.advsdk.util.LogUtils;
import com.yuchen.advsdk.weight.AdvPercentView;

public class MainActivity extends AppCompatActivity {

    AdvPercentView advPercentView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        advPercentView = findViewById(R.id.advview);
        //初始化
        LayoutPollManager.getInstance().init(this);

        //绑定视图
        LayoutPollManager.getInstance().bindAdvPercentView(advPercentView);

        //设置授权回调
        LayoutPollManager.getInstance().setAuthCallBack(new LayoutPollManager.AuthCallBack() {
            @Override
            public void start() {
//                LogUtils.d("start");
            }

            @Override
            public void success() {
                //在授权成功后，启动广告
                try {
                    LayoutPollManager.getInstance().startRun();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            @Override
            public void error(String msg) {
//                LogUtils.d("error >> "+msg);
            }
        });
        //配置
        LayoutPollManager.Config config = LayoutPollManager.getInstance().getConfig();
        //广告更新间隔，一般为 60秒
        config.setDuration(60 * 1000);
        //模式，在线与离线，本sdk 暂时仅支持在线模式
        config.setViewModel(AdvPercentView.Model.ON_LINE);
        LayoutPollManager.getInstance().setConfig(config);
        //注册回到
        LayoutPollManager.getInstance().setRegistCallBack(new LayoutPollManager.RegistCallBack() {
            @Override
            public void success(long deviceId, String deviceCode) {

            }
            @Override
            public void error(String errorMsg) {
//                LogUtils.d("error >> "+errorMsg);
            }
        });
        //授权参数，联系商务获取
        String appId = "1";
        String appSecret = "2";
        String autoCode = "3";
        //开始注册
        LayoutPollManager.getInstance().regist(appId, appSecret, autoCode);
    }
    @Override
    protected void onPause() {
        super.onPause();
        LayoutPollManager.getInstance().onPause();
    }
    @Override
    protected void onResume() {
        super.onResume();
            try {
                LayoutPollManager.getInstance().onResume();
            } catch (Exception e) {
                e.printStackTrace();
            }
    }
    @Override
    protected void onStop() {
        super.onStop();
        LayoutPollManager.getInstance().stopRun();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        LayoutPollManager.getInstance().bindAdvPercentView(advPercentView);
        LayoutPollManager.getInstance().onDestory();
    }
}